package com.victor.controller.app;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.victor.entity.city.CityIntroduce;
import com.victor.entity.city.CityIntroduceComment;
import com.victor.entity.city.CityIntroduceCommentPhoto;
import com.victor.entity.city.CityIntroduceCommentPraise;
import com.victor.entity.city.CityIntroduceCommentReply;
import com.victor.entity.city.CityIntroducePraise;
import com.victor.service.CityService;
import com.victor.utils.Msg;

/**
 * app端获取城市介绍
 * 
 * @author qinyuquan
 *
 */
@RestController
@RequestMapping("/cityapp")
public class CityAppController {

	@Autowired
	private CityService service;

	/**
	 * 传入城市名返回该城市所有信息
	 * @param city	城市名称
	 * @param userid 当前登录用户id
	 * @return
	 */
	@RequestMapping("/getCityMessage")
	public Msg getCityMessage(String city, String userid) {
		if (city == null || city.equals("") || userid == null || userid.equals("")) {
			return Msg.fail("参数错误");
		}
		// 获取城市信息
		CityIntroduce cityIntroduce = service.getCityMessage(city);
		if (null == cityIntroduce) {
			return Msg.fail("未查询到该城市记录");
		}
		// 根据城市id获取点赞数
		int praisenumbers = service.getPraisenumber(cityIntroduce.getC_id());
		// 设置城市点赞数
		cityIntroduce.setPraisenumber(praisenumbers);

		// 根据城市id获取评论数
		int comments = cityIntroduce.getCityIntroduceComments().size();
		// 设置城市评论数
		cityIntroduce.setCommentnumber(comments);

		// 查询登录人是否点赞该城市
		int ifpraisecity = service.ifPraiseCityToUserid(cityIntroduce.getC_id(), userid);

		cityIntroduce.setIfpraisecity(ifpraisecity);

		// 获取每条评论的点赞数和评论数
		for (int i = 0; i < cityIntroduce.getCityIntroduceComments().size(); i++) {
			// 获取评论的回复数
			int commentReplynumbers = cityIntroduce.getCityIntroduceComments().get(i).getCityIntroduceCommentReplys()
					.size();
			// 设置城市评论的回复数
			cityIntroduce.getCityIntroduceComments().get(i).setCommentnumber(commentReplynumbers);
			// 获取评论的点赞数
			for (int j = 0; j < commentReplynumbers; j++) {
				int commentpraisenumbers = service.getCommentPraisenumber(cityIntroduce.getCityIntroduceComments()
						.get(i).getCityIntroduceCommentReplys().get(j).getCcr_id());
				cityIntroduce.getCityIntroduceComments().get(i).setPraisenumber(commentpraisenumbers);
				// 查询登陆人是否点赞该回复
				CityIntroduceCommentPraise ci = service.ifPraise(userid);
				if (ci != null) {
					cityIntroduce.getCityIntroduceComments().get(i).setIfpraise("1");
				} else {
					cityIntroduce.getCityIntroduceComments().get(i).setIfpraise("0");
				}
			}
		}
		return Msg.success().add("cityIntroduce", cityIntroduce);
	}

	/**
	 * 评论城市
	 * @param cityIntroduceComment
	 * c_id 城市id
	 * userid 评论人id
	 * cc_content 评论内容
	 * @return
	 */
	@RequestMapping("commentCity")
	public Msg commentCity(@RequestBody CityIntroduceComment cityIntroduceComment) {
		if (cityIntroduceComment == null || cityIntroduceComment.getC_id() == null
				|| cityIntroduceComment.getUserid() == null) {
			return Msg.fail("参数错误");
		}
		// 添加评论
		boolean b = service.commentCity(cityIntroduceComment);
		if (!b) {
			return Msg.fail("添加失败");
		}
		//判断城市评论中没有图片，没有图片直接返回添加评论结果
		if (cityIntroduceComment.getCityIntroduceCommentPhotos()==null) {
			return Msg.judge(b);
		}
		// 添加评论的图片
		for (int i = 0; i < cityIntroduceComment.getCityIntroduceCommentPhotos().size(); i++) {
			//将返回城市评论表id，设置给城市评论图片中的cc_id
			cityIntroduceComment.getCityIntroduceCommentPhotos().get(i).setCc_id(cityIntroduceComment.getCc_id()+"");
			boolean bo = service.commentCityPhoto(cityIntroduceComment.getCityIntroduceCommentPhotos().get(i));
			if (!bo) {
				return Msg.fail("添加图片失败");
			}
		}
		return Msg.success();
	}

	/**
	 * 城市介绍评论回复
	 * @param cityIntroduceCommentReply
	 * cc_id 城市评论表id
	 * userid 城市评论回复人id
	 * ccr_content 城市评论回复内容（不能为空字符串）
	 * @return
	 */
	@RequestMapping("replyComment")
	public Msg replyComment(CityIntroduceCommentReply cityIntroduceCommentReply) {
		if (cityIntroduceCommentReply == null || cityIntroduceCommentReply.getCc_id() == null
				|| cityIntroduceCommentReply.getUserid() == null || cityIntroduceCommentReply.getCcr_content() == null
				|| cityIntroduceCommentReply.getCcr_content().equals("")) {
			return Msg.fail("参数错误");
		}
		// 添加评论
		boolean b = service.replyComment(cityIntroduceCommentReply);
		return Msg.judge(b);
	}

	/**
	 * 城市介绍点赞
	 * @param cityIntroducePraise
	 * userid 城市介绍点赞人
	 * c_id 城市介绍表id
	 * @return
	 */
	@RequestMapping("praiseCity")
	public Msg praiseCity(CityIntroducePraise cityIntroducePraise) {
		if (cityIntroducePraise == null || cityIntroducePraise.getUserid() == null
				|| cityIntroducePraise.getC_id() == null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.praiseCity(cityIntroducePraise);
		return Msg.judge(b);
	}

	/**
	 * 城市介绍评论点赞
	 * @param cityIntroduceCommentPraise
	 * userid 城市介绍评论点赞人id
	 * cc_id 城市介绍评论表id
	 * @return
	 */
	@RequestMapping("commentPraise")
	public Msg commentPraise(CityIntroduceCommentPraise cityIntroduceCommentPraise) {
		if (cityIntroduceCommentPraise == null || cityIntroduceCommentPraise.getUserid() == null
				|| cityIntroduceCommentPraise.getCc_id() == null) {
			return Msg.fail("参数错误");
		}
		boolean b = service.commentPraise(cityIntroduceCommentPraise);
		return Msg.judge(b);
	}
}
